import { PageLoading } from '@ant-design/pro-layout';
import { history, Link } from 'umi';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { currentUser as queryCurrentUser } from './services/doa/user';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import {requestInterceptor, responseInterceptor} from './utils/BDConfig'
import BDStorageUtils from '@/utils/BDStorageUtils'



const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
/** 获取用户信息比较慢的时候会展示一个 loading */


export const request = {
  requestInterceptors:[requestInterceptor],
  responseInterceptors:[responseInterceptor],
}

export const initialStateConfig = {
  loading: <PageLoading />,
};
/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state,全局初始化
 * */
//全局初始化的地方，可以再这里放一些全局的数据，参考https://pro.ant.design/zh-CN/docs/initial-state
export async function getInitialState() {
  const fetchUserInfo = async (data) => {
    try {
      const msg = await queryCurrentUser(data);
      return msg.data;
    } catch (error) {
      history.push(loginPath);
    }

    return undefined;
  };

  console.log("get initial state: " + history.location.pathname)
  // 如果不是登陆界面并且不是注册界面
  if (!history.location.pathname.startsWith(loginPath) && history.location.pathname !== "/user/register") {
    const localStorage = BDStorageUtils.getUsers()
    const currentUserData = await fetchUserInfo({doi:localStorage?.doi, role:localStorage?.role});
    const currentUser = {...JSON.parse(currentUserData),access:JSON.parse(currentUserData)?.handleValues?.access}
    return {
      fetchUserInfo,
      currentUser,
      settings: {},
    };
  }

  return {
    fetchUserInfo,
    settings: {},
  };
} // ProLayout 支持的api https://procomponents.ant.design/components/layout

export const layout = ({ initialState }) => {
  return {
    rightContentRender: () => <RightContent />,
    disableContentMargin: false,
    waterMarkProps: {
      content: initialState?.currentUser?.identifier,
    },
    footerRender: () => <Footer />,
    onPageChange: () => {
      const { location } = history; // 如果没有登录，重定向到 login

      if ((location.pathname !== "/setting" && location.pathname !== "/user/register" && location.pathname !== "/grs/apply" && location.pathname !== "/irs/apply") && !initialState?.currentUser && !location.pathname.startsWith(loginPath)) {
        history.push(loginPath);
      }
    },
    links: isDev
      ? [
        //   <Link to="/umi/plugin/openapi" target="_blank">
        //     <LinkOutlined />
        //     <span>北大数瑞区块链 文档</span>
        //   </Link>,
        //   <Link to="http://39.104.204.99:21030/OnlineIDE.html" target="_blank">
        //     <LinkOutlined />
        //     <span>合约 IDE</span>
        //   </Link>,
        // <Link to="http://39.104.204.99:21030/NodePortal.html" target="_blank">
        //   <LinkOutlined />
        //   <span>合约 节点管理</span>
        // </Link>,
        //   <Link to="/umi/plugin/openapi" target="_blank">
        //     <LinkOutlined />
        //     <span>OpenAPI 文档</span>
        //   </Link>,
        //   <Link to="/~docs">
        //     <BookOutlined />
        //     <span>业务组件文档</span>
        //   </Link>,
        ]
      : [],
    menuHeaderRender: undefined,
    // 自定义 403 页面
    // unAccessible: <div>unAccessible</div>,
    ...initialState?.settings,
  };
};
